package de.fhaugsburg.games.boardgames;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class GridPoint {

	public final int row;
	public final int col;

	public GridPoint(int row, int col) {
		this.row = row;
		this.col = col;
	}

	public String toString() {
		return "(row: " + row + ", col:" + col + ")";
	}

	public Object clone() {
		return new GridPoint(row, col);

	}

	/**
	 * 
	 * @return a new GridPoint with negated row and column.
	 */
	public GridPoint neg() {
		return new GridPoint(row * -1, col * -1);
	}

	/**
	 * Adds the rows and columns from the actual GridPoint with the given
	 * GridPoint and return a new GridPoint with the added values.
	 * 
	 * @param point
	 * @return
	 */
	public GridPoint add(GridPoint point) {
		return new GridPoint(row + point.row, col + point.col);

	}

	public boolean equals(Object o) {
		if (o instanceof GridPoint) {
			GridPoint g = (GridPoint) o;
			if (row == g.row && col == g.col)
				return true;
		}
		return false;
	}

	public GridPoint minus(GridPoint point) {
		return new GridPoint(row - point.row, col - point.col);
	}

	public boolean isManhattancolinearWith(Set<GridPoint> pointSet) {
		Iterator<GridPoint> iter = pointSet.iterator();
		int rowCount = 0;
		int colCount = 0;

		while (iter.hasNext()) {
			GridPoint g = iter.next();
			if (row == g.row)
				rowCount++;
			if (col == g.col)
				colCount++;
		}
		if (rowCount == pointSet.size() || colCount == pointSet.size())
			return true;
		else
			return false;
	}

}
